var apiurl = 'https://apifront.wenlewen.com'

$(function(){

    // 头部搜索按钮事件
    $('#header_search_btn').click(function (){
        var keyword = $('#header_search_input').val();
        if(keyword == ''){
            return;
        }
        window.location='/goods.html?keyword='+keyword;
    })

    //头部搜索框回车事件
    $('#header_search_input').on('keypress',function (e){
        var keyword = $('#header_search_input').val();
        if(keyword == ''){
            return;
        }
        if (e.keyCode != "13") {
            return;
        }
        window.location='/goods.html?keyword='+keyword;
    })

    //底部搜索框回车事件
    $('.search .search_box .search_input').on('keypress',function (e){
        var keyword = $('.search .search_box .search_input').val();
        if(keyword == ''){
            return;
        }
        if (e.keyCode != "13") {
            return;
        }
        window.location='/goods.html?keyword='+keyword;
    })

    // 头部购买弹出层
    $('.header .buy-box').hover(function (){
        $('.header .buy-box .sub').show();
    },function (){
        $('.header .buy-box .sub').hide();
    })

    //租赁页面表单提交
    $('.lease_submit_btn').click(function (){
        opt_btn = $(this);
        if($('select[name=printer_id]').val() == ''){
            layer.open({type: 4,time:1500,content: ['请选择打印机型号！',opt_btn]});return ;
        }
        if($('input[name=name]').val() == ''){
            layer.open({type: 4,time:1500,content: ['请填写姓名！',opt_btn]});return ;
        }
        if($('input[name=company_name]').val() == ''){
            layer.open({type: 4,time:1500,content: ['请填写公司名称！',opt_btn]});return ;
        }
        if($('input[name=mobile]').val() == ''){
            layer.open({type: 4,time:1500,content: ['请填写电话！',opt_btn]});return ;
        }
        if($('input[name=address]').val() == ''){
            layer.open({type: 4,time:1500,content: ['请填写联系地址！',opt_btn]});return ;
        }
        if($('input[name=start_time]').val() == ''){
            layer.open({type: 4,time:1500,content: ['请填写开始时间！',opt_btn]});return ;
        }
        if($('input[name=end_time]').val() == ''){
            layer.open({type: 4,time:1500,content: ['请填写结束时间！',opt_btn]});return ;
        }
        $.ajax({
            url:'/portal/index/printer_apply',
            data:{
                printer_id:$('select[name=printer_id]').val(),
                name:$('input[name=name]').val(),
                company_name:$('input[name=company_name]').val(),
                mobile:$('input[name=mobile]').val(),
                address:$('input[name=address]').val(),
                start_time:$('input[name=start_time]').val(),
                end_time:$('input[name=end_time]').val(),
                remark:$('input[name=remark]').val(),
                },
            type:"post",
            success:function (res){
                if(res.code != 1){
                    return layer.open({title: '错误',content: '申请失败：'+ res.msg});
                } else {
                    return layer.open({title: '成功',content: '申请成功!'});
                }
            },
            error:function (res){
                return layer.open({title: '错误',content: '网络好像不通了，稍后请重试！'});
            }
        })
    })


    //登录密码和验证码切换
    $('.login_tab .item').click(function (){
        var type = $(this).attr('type');
        $('.login_tab .item').removeClass('active');
        $(this).addClass('active');
        if(type == 'code') {
            $('.login_box .item-code').show();
            $('.login_box .item-password').hide();
        } else {
            $('.login_box .item-code').hide();
            $('.login_box .item-password').show();
        }
    })

    //登录点击事件
    $('.login_btn').click(function (){
        var account = $('input[name=account]').val();
        var password = $('input[name=password]').val();
        var type = $('.login_tab .item.active').attr('type');
        var code = $('input[name=code]').val();
        var key = $('.send_code').attr("key");
        if(type == 'code') {
            if(key == ''){
                return layer.open({title: '错误',content: '请发送验证码！'});
            }
            if(code == ''){
                return layer.open({title: '错误',content: '验证码不能为空！'});
            }
        } else {
            if(password == ''){
                return layer.open({title: '错误',content: '密码不能为空！'});
            }
        }
        if(account == ''){
            return layer.open({title: '错误',content: '手机不能为空！'});
        }


        if(type == 'code') {
            var request_data = {account:account,code:code,key:key,type:type}
        } else {
            var request_data = {account:account,password:password,type:type}
        }
        $.ajax({
            url:'/portal/index/login',
            data:request_data,
            type:"post",
            success:function (res){
                if(res.code != 1){
                    return layer.open({title: '错误',content: '登陆失败！'});
                }
                window.location='/';
            },
            error:function (res){

            }
        })
    })

    // 退出登录
    $('#logout_btn').click(function (){
        $.ajax({
            url:'/portal/index/logout',
            data:{},
            success:function (res){
                if(res.code != 1){
                    return layer.open({title: '错误',content: '退出失败！'});
                }
                window.location.reload();
            },
            error:function (res){

            }
        })
    })

    //重置密码点击事件
    $('.reset_btn').click(function () {
        var old_password = $('input[name=old_password]').val();
        var password = $('input[name=password]').val();
        var confirm_password = $('input[name=confirm_password]').val();
        if(old_password == ''){
            return layer.open({title: '错误',content: '原密码不能为空！'});
        }
        if(password == ''){
            return layer.open({title: '错误',content: '密码不能为空！'});
        }
        if(confirm_password == ''){
            return layer.open({title: '错误',content: '确认密码不能为空！'});
        }
        if(confirm_password != password){
            return layer.open({title: '错误',content: '新密码两次输入不一致！'});
        }
        var request_data = {old_password:old_password,password:password,confirm_password:confirm_password}
        $.ajax({
            url:'/portal/index/reset_password',
            data:request_data,
            type:"post",
            success:function (res){
                if(res.code != 1){
                    return layer.open({title: '错误',content: '重置失败！'});
                }
                window.location='/';
            },
            error:function (res){

            }
        })
    })

    // 租赁订单操作
    $('.lease_order_table .lease_cancle_btn').click(function () {
        var id = $(this).attr('data-id');
        $.ajax({
            url:'/portal/index/lease_cancle',
            data:{id:id},
            type:"post",
            success:function (res){
                if(res.code != 1){
                    return layer.open({title: '错误',content: '申请失败！'});
                }
                window.location.reload()
            },
            error:function (res){

            }
        })
    })

    // 租赁订单操作
    $('.lease_order_table .lease_end_btn').click(function () {
        var id = $(this).attr('data-id');
        $.ajax({
            url:'/portal/index/lease_end',
            data:{id:id},
            type:"post",
            success:function (res){
                if(res.code != 1){
                    return layer.open({title: '错误',content: '申请失败！'});
                }
                window.location.reload()
            },
            error:function (res){

            }
        })
    })

})